package com.zjeav.lingjiao.ui.personCenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.dalimao.corelibrary.view.SelfDialog;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;
import com.zjeav.lingjiao.R;
import com.zjeav.lingjiao.base.QQTools.Util;
import com.zjeav.lingjiao.base.baseBean.BaseActivity;
import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.config.BaseConfig;
import com.zjeav.lingjiao.base.request.GetVersionRequest;
import com.zjeav.lingjiao.base.request.OtherLoginRequest;
import com.zjeav.lingjiao.base.response.VersionResponse;
import com.zjeav.lingjiao.base.response.WXName;
import com.zjeav.lingjiao.base.tools.SharedPreferencesUtils;
import com.zjeav.lingjiao.base.utils.DetectionUpdate;
import com.zjeav.lingjiao.base.utils.ErrorUtils;
import com.zjeav.lingjiao.base.utils.StringUtils;
import com.zjeav.lingjiao.ui.home.persenter.GetVersionPersenter;
import com.zjeav.lingjiao.ui.home.view.GetVersionContract;
import com.zjeav.lingjiao.ui.login.LoginActivty;
import com.zjeav.lingjiao.ui.personCenter.view.SysContract;
import com.zjeav.lingjiao.ui.presenter.SysPresenter;

import org.json.JSONException;
import org.json.JSONObject;

public class SysSetActivity extends BaseActivity implements View.OnClickListener,
        SysContract.View, IUiListener,GetVersionContract.View {
    private RelativeLayout wx_rl;
    private TextView wx_name_txt, feedback_txt, sys_ver_txt, unregister_txt;
    SysPresenter sysPresenter;
    Intent intent;
    private SelfDialog selfDialog;
    private IWXAPI api;
    private Switch sv;
    GetVersionPersenter getVersionPersenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sys_seting_layout);
        init();
    }

    private void init() {
        wx_rl = findViewById(R.id.wx_rl);
        wx_name_txt = findViewById(R.id.wx_name_txt);
        feedback_txt = findViewById(R.id.feedback_txt);
        sys_ver_txt = findViewById(R.id.sys_ver_txt);
        sys_ver_txt.setOnClickListener(this);
        unregister_txt = findViewById(R.id.unregister_txt);
        wx_rl.setOnClickListener(this);
        feedback_txt.setOnClickListener(this);
        unregister_txt.setOnClickListener(this);
        sysPresenter = new SysPresenter(this);
        sv = findViewById(R.id.s_v);

        getVersionPersenter = new GetVersionPersenter(this);

        boolean switchopen = (boolean) SharedPreferencesUtils.getParam(this, "is4G", false);
        if (switchopen){
            sv.setChecked(true);
        }
        sv.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) { //打开
                    SharedPreferencesUtils.setParam(SysSetActivity.this, "is4G", true);
                } else {
                    SharedPreferencesUtils.setParam(SysSetActivity.this, "is4G", false);
                }
            }
        });
        regToWx();
    }

    @Override
    protected void onResume() {
        super.onResume();
        sysPresenter.getVersion(new GetVersionRequest("Android"));
    }

    private void regToWx() {
        api = WXAPIFactory.createWXAPI(this, BaseConfig.APP_ID, true);
        api.registerApp(BaseConfig.APP_ID);
    }

    private void WXlogin() {
        SharedPreferencesUtils.setParam(this, "weChat", 1);
        final SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "com_zjeav_lingjiao";
        api.sendReq(req);
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.wx_rl:
                WXlogin();
                break;
            case R.id.unregister_txt:
                selfDialog = new SelfDialog(this);
                selfDialog.setTitle("提示");
                selfDialog.setMessage("注销时将会清除账户信息,是否注销?");
                selfDialog.setYesOnclickListener("确定", new SelfDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        selfDialog.dismiss();
                        exitApp();
                        SharedPreferencesUtils.setParam(SysSetActivity.this, "username", "");
                        SharedPreferencesUtils.setParam(SysSetActivity.this, "password", "");

                        Intent intent = new Intent(SysSetActivity.this, LoginActivty.class);
                        startActivity(intent);
                        finish();
                    }
                });
                selfDialog.setNoOnclickListener("取消", new SelfDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        selfDialog.dismiss();
                    }
                });
                selfDialog.show();
                break;
            case R.id.feedback_txt:
                intent = new Intent(this, FeedbackActivity.class);
                startActivity(intent);
                break;
            case R.id.sys_ver_txt:
                GetVersionRequest getVersionRequest = new GetVersionRequest("Android");
                getVersionPersenter.getVersion(getVersionRequest);
                break;
        }
    }

    @Override
    public void getVersion(VersionResponse versionResponse) {
        if (versionResponse != null) {
            sys_ver_txt.setText(StringUtils.getAppVersionName(this));
        }
    }

    @Override
    public void bindWeChat(WXName response) {
        wx_name_txt.setText(response.getWechatname());
    }

    @Override
    public void showError(Throwable throwable, String message) {
        Toast.makeText(this, ErrorUtils.showError(throwable), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onComplete(Object o) {
        if (null == o) {
            Util.showResultDialog(SysSetActivity.this, "返回为空", "登录失败");
            return;
        }
        JSONObject jsonResponse = (JSONObject) o;
        if (null != jsonResponse && jsonResponse.length() == 0) {
            Util.showResultDialog(SysSetActivity.this, "返回为空", "登录失败");
            return;
        }
//        QQLoginResponse qqLoginResponse=(QQLoginResponse) jsonResponse;
        OtherLoginRequest request = null;
        try {
            request = new OtherLoginRequest(jsonResponse.get("access_token").toString(), jsonResponse.get("openid").toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        sysPresenter.bindWechat(request);
//  /      Util.showResultDialog(LoginActivty.this, o.toString(), "登录成功");
    }

    @Override
    public void onError(UiError uiError) {
        Util.toastMessage(SysSetActivity.this, "onError: " + uiError.errorDetail);
        Util.dismissDialog();
    }

    @Override
    public void onCancel() {
        Util.toastMessage(SysSetActivity.this, "onCancel: ");
        Util.dismissDialog();
    }

    @Override
    public void showGetVersion(Result<VersionResponse> responseResult) {
        DetectionUpdate.checkApp(responseResult,this,"mine");
    }

    @Override
    public void ShowError(Throwable throwable) {

    }
}
